package sanphone.com.runningschool;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import sanphone.com.runningschool.RequestHttp.RequestHttp;

public class ComplaintActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar toolbar;
    private Button complaint_submit;
    private EditText content;
private SharedPreferences sprf;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint);
        sprf=getSharedPreferences("data",MODE_PRIVATE);
        userId = sprf.getString("schoolId",null);
        initView();
    }

    private void initView() {
        toolbar =  findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        complaint_submit = findViewById(R.id.complaint_submit);
        content=findViewById(R.id.c_content);
        complaint_submit.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                ComplaintActivity.this.finish();
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complaint_submit:
                    submitContent();
                break;
        }
    }

    private void submitContent() {
        final String complaint=content.getText().toString();
        try {
            final String sendComplaint= URLEncoder.encode(complaint,"UTF-8");

        ExecutorService executorService= Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                JSONObject json=new JSONObject();
                try {
                    json.put("userId",userId);
                    json.put("adviceTitle",null);
                    json.put("adviceText",sendComplaint);
                    RequestHttp requestHttp=new RequestHttp(InterfaceLocation.getLocation()+"adviceInsert",ComplaintActivity.this);
                    String response=requestHttp.post(json);
                   JSONObject jsonObject=new JSONObject(response);
                   String data=jsonObject.getString("data");
                    Log.e("Complaint: ",data );
                   if(data.equals("意见提交成功")){
                       runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               Toast.makeText(ComplaintActivity.this,"感谢您的反馈！",Toast.LENGTH_SHORT).show();
                               try {
                                   Thread.sleep(1000);
                                   ComplaintActivity.this.finish();
                               } catch (InterruptedException e) {
                                   e.printStackTrace();
                               }

                           }
                       });

                   }else{
                       runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               Toast.makeText(ComplaintActivity.this,"提交失败噢，请重试！",Toast.LENGTH_SHORT).show();
                           }
                       });
                   }
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        executorService.shutdown();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}
